package demo10;

import java.util.Objects;

public class HashBucket<K,V> {
    static class Node<K,V> {
        private K key;
        private V value;
        private Node next;

        public Node(K key, V value) {
            this.key = key;
            this.value = value;
        }

    }


    private Node[] array;
    private int size;   // 当前的数据个数
    private static final double DEFAULT_LOAD_FACTOR = 0.75;
    private static final int DEFAULT_SIZE = 8;//默认桶的大小

    public HashBucket() {
        // write code here
        array = new Node[DEFAULT_SIZE];
    }
    public void put(K key, V value) {
        int hashCode = key.hashCode();
        int index = hashCode % array.length;
        Node<K,V> cur = array[index];
        while(cur != null) {
            if(cur.key.equals(key)) {
                cur.key = key;
                break;
            }
            cur = cur.next;
        }
        Node<K,V> node = new Node<>(key, value);
        if(array[index] == null) {
            array[index] = node;
        }else {
            node.next = array[index];
            array[index] = node;
        }
        size++;
    }


    public V get(K key) {
        int hashCode = key.hashCode();
        int index = hashCode % array.length;
        Node<K,V> cur = array[index];
        while(cur != null) {
            if(cur.key.equals(key)) {
                return cur.value;
            }
            cur = cur.next;
        }
        return null;
    }
}
